package cn.jhc.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jhc.bean.User;
import cn.jhc.bean.UserCookie;
import cn.jhc.dao.AccountDAO;
import cn.jhc.dao.DaoType;
import cn.jhc.factory.DAOFactory;

/**
 * Servlet implementation class UpdateBalanceServlet
 */
@WebServlet("/user/updatebalance.do")
public class UpdateBalanceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateBalanceServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String price = (String) request.getSession().getAttribute("prices");
		Double balance = Double.parseDouble(price);
		String creditcard = request.getParameter("choose");
		
		Cookie[] cookies = request.getCookies();
		User user = (User)request.getSession().getAttribute("currentUser");
		String username = user.getLoginname();
		Cookie c = UserCookie.tranversal(cookies, username);
		if(c != null && !(c.getValue().equals("")))
		{
			c.setValue("");
			response.addCookie(c);
			
		}
		
		AccountDAO dao = (AccountDAO)DAOFactory.getDao(DaoType.ACCOUNT);	
		dao.updateAccount(balance, creditcard);	
		response.sendRedirect("../list.htm");
	}

}
